import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import _Drawer from "@mui/material/Drawer";
import _Popover from "@mui/material/Popover";
import React, { useEffect, useMemo, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import styles from "./index.module.less";
import Loading from "../loading";
import View from "../../wallet";
import { useIsomorphicLayoutEffect, useMount, useUpdateEffect } from "ahooks";
import useServices from "../../hooks/useRequest";
import { EVENT, KEYCONST } from "../../enums";
import { useLocalStorageSetState } from "../../hooks";
import { getUserInfo } from "../../api/user";
import classNames from "classnames";
import { walletRootRef } from "../../utils";
import { getMyInviter } from "../../api/user";
import kingsInstance from "../../instance";
import { twitterAuth } from "../../api/auth";
import authConfig from "../../authServices/config";
import i18n from "../../local";
import PermissionWrapper from "../../provider/permission";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function KingWallet(props) {
    var getinfo = function getinfo() {
        services.getUserInfo().then(function(res) {
            localStorage.setItem(KEYCONST.userInfo, JSON.stringify(res.data));
            dispatch.app.SET_USER_INFO(res.data);
            dispatch.app.SET_LOGIN_STATUS(true);
        }).catch(function(err) {
            // console.log('err', err);
            localStorage.removeItem(KEYCONST.token);
            dispatch.app.SET_LOGIN_STATUS(false);
        });
        services.getMyInviter().then(function(res) {
            dispatch.app.SET_INVITER(res.data);
        });
    };
    var _props_PaperProps;
    var _useSelector = useSelector(function(state) {
        return {
            meta: state.app.meta,
            walletVisible: state.app.walletVisible,
            triggerEl: state.app.triggerEl,
            theme: state.app.theme,
            currentLocal: state.app.currentLocal
        };
    }), meta = _useSelector.meta, walletVisible = _useSelector.walletVisible, triggerEl = _useSelector.triggerEl, theme = _useSelector.theme, currentLocal = _useSelector.currentLocal;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), localState = _useLocalStorageSetState[0], setLocalState = _useLocalStorageSetState[1];
    var dispatch = useDispatch();
    var _useState = _sliced_to_array(useState(""), 2), centerClass = _useState[0], setCenterClass = _useState[1];
    // const container = useRef<HTMLDivElement | null>(null);
    var services = useServices({
        getUserInfo: getUserInfo,
        getMyInviter: getMyInviter
    });
    useIsomorphicLayoutEffect(function() {
        //判断是否是通过redirect跳转过来的
        var params = new URLSearchParams(location.search);
        if (params.get("code") && params.get("state") === "twitter") {
            twitterAuth({
                onaAppId: authConfig.ONA_APPID,
                onaAppSecret: authConfig.ONA_APP_SECRET,
                code: params.get("code")
            }).then(function(res) {
                localStorage.setItem(KEYCONST.token, JSON.stringify({
                    accessToken: res.data.accessData.token
                }));
            // getinfo();
            });
        } else {
        // getinfo();
        }
    }, []);
    // const onOpen = () => {
    //   dispatch.app.SWITCH_VISIBLE(true);
    // };
    var onClose = function() {
        dispatch.app.SWITCH_VISIBLE(false);
    };
    var switchVisible = function() {
        dispatch.app.SWITCH_VISIBLE(!walletVisible);
    };
    var body = /*#__PURE__*/ _jsx("div", {
        className: meta.fullScreen ? styles.full : styles.body,
        id: "#wallet",
        ref: walletRootRef,
        "data-theme": theme,
        children: /*#__PURE__*/ _jsx(View, {})
    });
    var _useMemo = useMemo(function() {
        if (meta.fullScreen) {
            return {
                popoverVisible: false,
                drawerVisible: walletVisible
            };
        }
        if (walletVisible === false) {
            setLocalState({
                chain: "",
                chainNft: ""
            });
        }
        return {
            popoverVisible: walletVisible,
            drawerVisible: false
        };
    }, [
        walletVisible,
        meta
    ]), popoverVisible = _useMemo.popoverVisible, drawerVisible = _useMemo.drawerVisible;
    useEffect(function() {
        /** 用来兜底显示位置 */ if (!props.anchorEl && !triggerEl && popoverVisible) {
            setCenterClass(styles.popover_center);
        } else {
            if (centerClass) {
                setTimeout(function() {
                    setCenterClass("");
                }, 500);
            }
        }
    }, [
        popoverVisible,
        props.anchorEl,
        triggerEl
    ]);
    /** 设置币种信息 */ useMount(function() {
        // dispatch.app.init('test','meta')
        i18n.on("languageChanged", function(lang) {
            //同步state状态
            localStorage.setItem(KEYCONST.language, lang);
            dispatch.app.SET_CURRENT_LOCAL(lang);
        });
        // dispatch.app.setCoinInfos();
        kingsInstance.on(EVENT.changeChainType, function(chainType) {
            window.CHAIN_TYPE = chainType;
            dispatch.app.setCoinInfos();
        });
        kingsInstance.on(EVENT.networkChanged, function(testnet) {
            window.TEST_NET = testnet;
            dispatch.app.SET_NETWORK(testnet);
        });
        kingsInstance.on(EVENT.envChange, function() {
            dispatch.app.setCoinInfos();
        });
    });
    // 监听state状态 同步语言环境
    useUpdateEffect(function() {
        if (i18n.language === currentLocal) return;
        i18n.changeLanguage(currentLocal);
    }, [
        currentLocal
    ]);
    return /*#__PURE__*/ _jsxs(PermissionWrapper, {
        children: [
            /*#__PURE__*/ _jsx(_Popover, _object_spread_props(_object_spread({
                open: popoverVisible,
                onClose: onClose,
                anchorOrigin: _object_spread({
                    vertical: "bottom",
                    horizontal: "right"
                }, props.anchorOrigin),
                transformOrigin: {
                    vertical: "top",
                    horizontal: "right"
                }
            }, props), {
                anchorEl: props.anchorEl || triggerEl,
                anchorPosition: {
                    top: 0,
                    left: 0
                },
                anchorReference: triggerEl || props.anchorEl ? "anchorEl" : "anchorPosition",
                className: classNames(props.className, centerClass),
                PaperProps: _object_spread_props(_object_spread({}, props.PaperProps), {
                    className: classNames((_props_PaperProps = props.PaperProps) === null || _props_PaperProps === void 0 ? void 0 : _props_PaperProps.className, styles.paper, "king-wallet-paper", theme === "dark" ? "king-wallet-dark" : "king-wallet-light")
                }),
                children: /*#__PURE__*/ _jsx("div", {
                    className: styles.custom_popover_content,
                    children: /*#__PURE__*/ _jsx(Loading, {
                        children: body
                    })
                })
            })),
            /*#__PURE__*/ _jsx(_Drawer, {
                anchor: "right",
                open: drawerVisible,
                onClose: onClose,
                children: /*#__PURE__*/ _jsx("div", {
                    className: styles["custom_drawer_content"],
                    children: /*#__PURE__*/ _jsx(Loading, {
                        children: body
                    })
                })
            })
        ]
    });
}
export default KingWallet;

 //# sourceMappingURL=index.js.map